﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.KMThuChi
{
    public partial class frmQuanLy : Form // VƯƠNG TRÍ LỢI
    {

        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion
        
        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            if (capnhat == true)
            {
                HienThi();
                if (GUI.KMThuChi.frmXuLy.CheckClose == true)
                {
                    Them();
                }
                GUI.NhanVien.frmXuLy.CheckClose = false;
              
            }
        }
        #endregion

        #region CheckPerMission
        private Boolean CheckPerMission(int active)
        {
            return Common.Utilities.Check(Common.FormName.Quanlykhoanmucthuchi, active);
        }
        #endregion

        #region Khoi Tao
        private Entities.KMThuChi khoanmuc;
        private string MaKM = string.Empty;
        private Entities.KMThuChi[] DanhSach;
        private bool CheckMa = true;
        private bool CheckTen = false;
        private Boolean CheckClose = false;

        public frmQuanLy()
        {
            InitializeComponent();
            Event();
        }
        private void Event()
        {
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            rdbTimMa.Checked = true;
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Sua();
            }
        }
       
        
        #endregion

        #region Event
         private Boolean kiemtra = false;

        private void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (CheckClose == false)
                {
                    System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (ketqua == System.Windows.Forms.DialogResult.Yes)
                        {
                            e.Cancel = false;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
                GetData(true);
            }
            catch (Exception)
            { }
        }

        private void rdbTimTen_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckTen = (bool)rdbTimTen.Checked;
                if (CheckTen == true)
                {
                    CheckMa = false;
                }
                else
                { CheckMa = true; }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void rdbTimMa_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckMa = (bool)rdbTimMa.Checked;
                if (CheckMa == false)
                {
                    CheckTen = true;
                }
                else
                { CheckTen = false; }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                SearchData(this.CheckTen, this.CheckMa, dgvHienThi, txtTimKiem.Text, this.DanhSach);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                Entities.KMThuChi[] k = new Entities.KMThuChi[0];
                dgvHienThi.DataSource = k;
            }
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Sua();
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            //lay ma nhan vien tren gidview
            MaKM = dgvHienThi.CurrentRow.Cells["MaKhoanMuc"].Value.ToString();
            this.GetRow();

        }


        private void GetRow()
        {
            try
            {
                if (DanhSach == null) { return; }
                for (int i = 0; i < this.DanhSach.Length; i++)
                {
                    if (DanhSach[i].MaKhoanMuc == this.MaKM)
                    {
                        khoanmuc = DanhSach[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            this.Them();
        }

         private void btnSua_Click(object sender, EventArgs e)
        {
            this.Sua();
        }

         private void btnXoa_Click(object sender, EventArgs e)
        {
            this.Xoa();
        }

         private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

         private void frmQuanLy_KeyDown(object sender, KeyEventArgs e)
         {
        }
        #endregion

        #region XuLy


         #region Tim Kiem
         private void SearchData(bool rdoMa, bool rdoTen, DataGridView dgv, string giatritim, Entities.KMThuChi[] mangtim)
         {
             try
             {
                 if (giatritim != string.Empty)
                 {
                     if (mangtim.Length > 0)
                     {
                         ArrayList list = new ArrayList();
                         Entities.KMThuChi[] tt1_search = null;
                         if (rdoTen == false)
                         {
                             if (mangtim == null)
                             {
                                 Entities.KMThuChi[] lay = new Entities.KMThuChi[0];
                                 dgv.DataSource = lay;
                             }
                             else
                             {
                                 for (int i = 0; i < mangtim.Length; i++)
                                 {
                                     int index = -1;
                                     index = mangtim[i].TenKhoanMuc.ToString().ToLower().IndexOf(giatritim.ToLower());
                                     if (index >= 0)
                                     {
                                         list.Add(mangtim[i]);
                                     }
                                 }
                                 if (list.Count <= 0)
                                 {
                                     Entities.KMThuChi[] lay = new Entities.KMThuChi[0];
                                     dgv.DataSource = lay;
                                 }
                                 else
                                 {
                                     int n = list.Count;
                                     if (n == 0) { tt1_search = null; }
                                     tt1_search = new Entities.KMThuChi[n];
                                     for (int i = 0; i < n; i++)
                                     {
                                         tt1_search[i] = (Entities.KMThuChi)list[i];
                                     }
                                     dgv.DataSource = tt1_search;
                                     list = null;
                                     tt1_search = null;
                                 }
                             }
                         }
                         if (rdoMa == false)
                         {
                             if (mangtim == null)
                             {
                                 Entities.KMThuChi[] lay = new Entities.KMThuChi[0];
                                 dgv.DataSource = lay;
                             }
                             else
                             {
                                 for (int i = 0; i < mangtim.Length; i++)
                                 {
                                     int index = -1;
                                     index = mangtim[i].MaKhoanMuc.ToString().ToLower().IndexOf(giatritim.ToLower());
                                     if (index >= 0)
                                     {
                                         list.Add(mangtim[i]);
                                     }
                                 }
                                 if (list.Count <= 0)
                                 {
                                     Entities.KMThuChi[] lay = new Entities.KMThuChi[0];
                                     dgv.DataSource = lay;
                                 }
                                 else
                                 {
                                     int n = list.Count;
                                     if (n == 0) { tt1_search = null; }
                                     tt1_search = new Entities.KMThuChi[n];
                                     for (int i = 0; i < n; i++)
                                     {
                                         tt1_search[i] = (Entities.KMThuChi)list[i];
                                     }
                                     dgv.DataSource = tt1_search;
                                     list = null;
                                     tt1_search = null;
                                 }
                             }
                         }
                     }
                     else
                     {
                         Entities.KMThuChi[] k = new Entities.KMThuChi[0];
                         dgv.DataSource = k;
                     }

                 }
                 else
                 {
                     dgv.DataSource = mangtim;
                 }
                 if (DanhSach.Length > 0)
                 {
                     //ucHienThiKhoanMucThuChi1.lvKhoanMucThuChi.SelectedIndex = 0;
                 }
             }
             catch (Exception ex)
             {
                 string s = ex.Message;
                 Entities.KMThuChi[] k = new Entities.KMThuChi[0];
                 dgv.DataSource = k;
             }
         }
         #endregion

         private void HienThi()
         {
             Entities.KMThuChi kmtc = new Entities.KMThuChi();
             kmtc.HanhDong = "0";
             // Common.StaticVariable.InterfaceObject cham' 1 cai se lay dc cac ham tu sql ma minh da tao trong interface
             DanhSach = Common.StaticVariable.InterfaceObject.SelectKMThuChi(kmtc);
             dgvHienThi.DataSource = DanhSach;
             //hien thi lai cai gidview
             HienThiList();
         }

         private void HienThiList()
        {
            try
            {
                if (DanhSach == null)
                {
                    Entities.KMThuChi gitri = new Entities.KMThuChi("0");
                    dgvHienThi.DataSource = gitri;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["HanhDong"].HeaderText = "STT";
                dgvHienThi.Columns["HanhDong"].Visible = true;
                dgvHienThi.Columns["MaKhoanMuc"].HeaderText = "Mã khoản mục";
                dgvHienThi.Columns["MaKhoanMuc"].Visible = true;
                dgvHienThi.Columns["TenKhoanMuc"].HeaderText = "Tên khoản mục";
                dgvHienThi.Columns["TenKhoanMuc"].Visible = true;
                dgvHienThi.Columns["DoiTuong"].HeaderText = "Đối tượng";
                dgvHienThi.Columns["DoiTuong"].Visible = true;
                dgvHienThi.Columns["NoTK"].HeaderText = "Nợ TK";
                dgvHienThi.Columns["NoTK"].Visible = true;
                dgvHienThi.Columns["CoTK"].HeaderText = "Có TK";
                dgvHienThi.Columns["CoTK"].Visible = true;
                dgvHienThi.Columns["TenLoaiKM"].HeaderText = "Tên loại KM";
                dgvHienThi.Columns["TenLoaiKM"].Visible = true;
                dgvHienThi.Columns["GhiChu"].HeaderText = "Ghi chú";
                dgvHienThi.Columns["GhiChu"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            }
            catch
            { }
        }
       
        private void Them()
        {
            try
            {
                if (CheckPerMission(1) != true)
                {
                    MessageBox.Show(Common.MSG.MSG10(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }

                    GUI.KMThuChi.frmXuLy frm = new GUI.KMThuChi.frmXuLy(Common.Constants.Insert, khoanmuc);
                    frm.GetData = new GUI.KMThuChi.frmXuLy.GetString(GetValue);
                    frm.Show();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void Sua()
        {
            try
            {
                if (CheckPerMission(2) != true)
                {
                    MessageBox.Show(Common.MSG.MSG9(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                if (DanhSach == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }
                if (this.DanhSach.Length > 0)
                {
                    for (int i = 0; i < DanhSach.Length; i++)
                    {
                        if (DanhSach[i].MaKhoanMuc == dgvHienThi.CurrentRow.Cells["MaKhoanMuc"].Value.ToString())
                        {
                            khoanmuc = DanhSach[i];
                        }
                    }

                    if (khoanmuc.MaKhoanMuc != null)
                    {
                        GUI.KMThuChi.frmXuLy frm = new GUI.KMThuChi.frmXuLy(Common.Constants.Update, this.khoanmuc);
                        frm.GetData = new GUI.KMThuChi.frmXuLy.GetString(GetValue);
                        frm.Show();

                    }
                    else
                    { MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe); return; }
                }
                else
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }
            }
            catch
            {
            }
        }

        private void Xoa()
        {
            try
            {
                if (CheckPerMission(3) != true)
                {
                    MessageBox.Show(Common.MSG.MSG9(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                if (DanhSach == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }

                if (this.DanhSach.Length > 0)
                {
                    Entities.CheckRefer cr = new Entities.CheckRefer();
                    cr.MaTruong = this.khoanmuc.TenKhoanMuc;
                    cr.TenTruong = "KhoanMucThuChi";

                    if (!Common.StaticVariable.InterfaceObject.CheckRefer(cr))
                    {
                        MessageBox.Show(cr.MaTruong + " đã có giao dịch không thể xóa", Common.MSG.TieuDe, MessageBoxButtons.OK);
                        return;
                    }

                    System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.MSG.MSG1(this.MaKM), Common.MSG.TieuDe, System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (giatri == System.Windows.Forms.DialogResult.Yes)
                        {
                            int k = Common.StaticVariable.InterfaceObject.DeleteKMThuChi(khoanmuc);
                            if (k != 0)
                            { 
                                HienThi();
                                this.MaKM = string.Empty;
                                MaKM = dgvHienThi.CurrentRow.Cells["MaKhoanMuc"].Value.ToString();
                                this.GetRow();
                            }
                            else
                            { MessageBox.Show("Xóa Thất bại", Common.MSG.TieuDe); }
                        }
                    }
                }
                else
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }

            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region FormLoad 
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            try
            {
                HienThi();
                this.WindowState = FormWindowState.Maximized;
                MaKM = dgvHienThi.CurrentRow.Cells["MaKhoanMuc"].Value.ToString();
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap; 
                this.GetRow();
            }
            catch
            { }
        }
        #endregion

    }
}
